package com.thenie.service.impl;


import com.thenie.mapper.EmployeeMapper;
import com.thenie.pojo.Employee;
import com.thenie.service.EmployeeService;
import com.thenie.utils.PasswordUtil;
import com.thenie.utils.SystemConstant;
import com.thenie.vo.EmployeeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 用户登录
     * @param loginName
     * @param loginPwd
     * @return
     */
    public Employee login(String loginName, String loginPwd) {
        //根据用户名查询员工信息
        Employee employee = employeeMapper.findEmployeeByLoginName(loginName);

        if(employee!=null){
            //如果存在
            //将用户的密码，进行加密然后比对。
            String newLoginPwd= PasswordUtil.md5(loginPwd,employee.getSalt(), SystemConstant.PASSWORD_COUNT);
            if(newLoginPwd.equals(employee.getLoginPwd())){
                //成功
                return employee;
            }
        }
        //失败
        return null;
    }

    /**
     * 根据部门编号查询员工数量
     * @param deptId
     * @return
     */
    public int findEmployeeCountByDeptId(int deptId){
        return employeeMapper.findEmployeeCountByDeptId(deptId);
    }

    /**
     * 根据角色编号查询员工数量
     * @param roleId
     * @return
     */
    public int findEmployeeCountByRoleId(int roleId){
        return employeeMapper.findEmployeeCountByRoleId(roleId);
    }

    /**
     * 查询所有员工
     * @param employeeVo
     * @return
     */
    public List<Employee> findAllEmployee(EmployeeVo employeeVo){
        return employeeMapper.findAllEmployee(employeeVo);
    }

    /**
     * 保存一条员工信息
     * @param employee
     * @return
     */
    public int saveEmployee(Employee employee) {
        //盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        //加密后的密码
        String loginPwd = PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD, salt, SystemConstant.PASSWORD_COUNT);
        //创建时间
        employee.setCreateDate(new Date());
        employee.setSalt(salt);
        employee.setLoginPwd(loginPwd);

        return employeeMapper.insertEmployee(employee);
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    public int updateEmployee(Employee employee){
        employee.setModifyDate(new Date());
        return employeeMapper.updateEmployee(employee);
    }

    /**
     * 根据id删除员工
     * @param employeeId
     * @return
     */
    public int deleteEmployee(int employeeId){
        employeeMapper.deleteEmployeeOfRole(employeeId);
        return employeeMapper.deleteEmployee(employeeId);
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    public int resetPwd(int id) {
        //盐值
        String salt = UUID.randomUUID().toString().replace("-", "");
        //密码
        String loginPwd = PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD, salt, SystemConstant.PASSWORD_COUNT);
        Employee employee = new Employee();
        employee.setId(id);
        employee.setSalt(salt);
        employee.setLoginPwd(loginPwd);
        return employeeMapper.resetPwd(employee);
    }

    /**
     * 查询员工下的角色
     * @param employeeId
     * @return
     */
    public List<Integer> findEmpOfRoleIdByEmpId(int employeeId){
        return employeeMapper.findEmpOfRoleIdByEmpId(employeeId);
    }

    /**
     * 保存员工和角色的关系
     * @param rIds
     * @param eid
     * @return
     */
    public int saveEmployeeOfRole(String[] rIds, int eid) {
        //删除原有的关系
        employeeMapper.deleteEmployeeOfRole(eid);
        //插入现有关系
        for (String rId : rIds) {
            employeeMapper.insertEmpOfRole(rId,eid);
        }
        return 1;
    }

    /**
     * 修改用户密码
     * @param inputPassword
     * @param newPassword
     * @param employee
     * @return
     */
    public int updatePassword(String inputPassword, String newPassword, Employee employee) {
        //对输入的密码进行加密
        String inputPassword1 = PasswordUtil.md5(inputPassword, employee.getSalt(), SystemConstant.PASSWORD_COUNT);
        //和原先的密码进行比对
        if (inputPassword1.equals(employee.getLoginPwd())){//相同
            String salt = UUID.randomUUID().toString().replace(",", "");//生成新的盐值
            String password = PasswordUtil.md5(newPassword, salt, SystemConstant.PASSWORD_COUNT);//生成密码
            employee.setSalt(salt);
            employee.setLoginPwd(password);
            return employeeMapper.updateEmployee(employee);//修改
        }
        return 0;
    }


}
